﻿using System.Collections;
using System.Collections.Generic;
using CSUtils;
using DG.Tweening;
using TMPro;
using TripleJump;
using U3dUtils;
using UI00;
using UnityEngine;
using UnityEngine.UI;

namespace G02.TripleJump.UI {
    /// <summary>
    /// 提示文本
    /// </summary>
    public class UI_Tips : UIBase {
        public static string prefabPath;

        protected override string _GetUIPrefabPath() {
            if (prefabPath != null) {
                return prefabPath;
            }
            return "TripleJump/UI/UI_Tips/UI_Tips.prefab";
        }

        protected override EUIType _uiType => EUIType.e04_main;
        public static Transform parent;

        protected override Transform _GetUIParent() {
            if (parent != null) {
                return parent;
            }
            return UIBase.uiLayer5_uiEffect;
        }

        public static UI_Tips GetInstance {
            get {
                if (instance == null) {
                    instance = UIManage.Open<UI_Tips>();
                }
                return instance;
            }
        }
        static UI_Tips instance;
        public TextMeshProUGUI tipsText;
        RectTransform newRecordTips;
        Text newRecordTipsText;

        protected override void _Init() {
            // base.Init();
            instance = this;
            tipsText = m_rootRt.GetControl<TextMeshProUGUI>("tipsText");
            tipsText.rectTransform.gameObject.SetActive(false);

            newRecordTips = GetControl<RectTransform>("newRecordTips");
            newRecordTipsText = GetControl<Text>("newRecordTips/newRecordTipsText");
            newRecordTips.gameObject.SetActive(false);

            //camera.transform.SetParent(UIBase.O_Canvas.transform);
            //effectImage.SetParent(UIBase.O_Canvas.transform);
        }

        protected override void _Open(Dictionary<string, object> paraDic) {
            m_rootRt.SetSiblingIndex(1000);
        }

        public void GameEnd() {
            newRecordTips.gameObject.SetActive(false);
        }

        public void SetLanguage() {
            newRecordTipsText.text = CL.G("新的最佳成绩");
        }

        const float duration = 0.9f, animTime = 0.3f;
        Tweener tweenerScale, tweenerColor;
        CDelayMain coroutine;

        public void OpenTips(string tipsContent, bool isColorAnim = false, bool isAnim = false, bool isTilt = false) {
            base.Open(null);
            Debug.LogFormat("UI_Tips_________________{0}", tipsContent);

            coroutine?.SetStop();

            if (tweenerScale != null) {
                tweenerScale.Pause();
            }
            if (tweenerColor != null) {
                tweenerColor.Pause();
            }

            tipsText.rectTransform.gameObject.SetActive(true);
            // rectTransform.SetSiblingIndex(1000);
            tipsText.text = tipsContent;

            if (isColorAnim) {
                tipsText.materialForRendering.color = Color.yellow;
                tweenerColor = tipsText.materialForRendering.DOColor(Color.green, animTime);
                o_LoopSetColorId++;
                UIBase.mono.StartCoroutine(LoopSetColor(o_LoopSetColorId));
            } else {
                tipsText.materialForRendering.color = Color.yellow;
            }
            float animTime0 = animTime;
            float localScale = 10;
            if (isAnim == false) {
                animTime0 = 0.15f;
                localScale = 4;
            }

            tipsText.transform.localScale = Vector3.one * localScale;
            tweenerScale = tipsText.transform.DOScale(Vector3.one, animTime0);

            if (isTilt) {
                tipsText.rectTransform.eulerAngles = Vector3.forward * 10;
            } else {
                tipsText.rectTransform.eulerAngles = Vector3.zero;
            }
            coroutine = CDelayMain.Delay(duration);
            coroutine.onCallback = delegate {
                tipsText.rectTransform.gameObject.SetActive(false);
            };
        }

        Color[] colors = new Color[] { Color.red, Color.yellow, Color.red, Color.yellow, Color.red, Color.yellow };
        float intervalTime = 0.08f;
        int o_LoopSetColorId;

        IEnumerator LoopSetColor(int loopSetColorId) {
            for (int i = 0; i < colors.Length; i++) {
                yield return new WaitForSeconds(intervalTime);
                if (loopSetColorId != o_LoopSetColorId) {
                    yield break;
                }
                tipsText.materialForRendering.SetColor("_FaceColor", colors[i]);
            }
        }

        public static void ShowNewRecordTips(int roloId, float distance) {
            if (TripleJumpData.curr.helpStep < 2) {
                return;
            }
            UI_Tips.GetInstance.newRecordTips.gameObject.SetActive(true);
            CDelayMain.Delay(3).SetPauseEnd("").onCallback = delegate {
                if (UI_Tips.GetInstance.newRecordTips.gameObject.activeSelf == true) {
                    UI_Tips.GetInstance.newRecordTips.gameObject.SetActive(false);
                }
            };
        }
    }
}
